<script setup lang="ts">
import { reactive, ref } from "vue";
import { useDialogView } from "@/hooks/useDialogView";
import { list, del } from "@/api/hnvmns-business-subsystem/room";
import { useDict } from "@/hooks/useDict";
import { FormData } from "./components/index";
import { filterOptionDict } from "@/utils/filters";
import { ElMessage, ElMessageBox } from "element-plus";
import type { FormOption } from "@/components/SearchForm";
const loading = ref<boolean>(false);
const { visible, type, id, handleView } = useDialogView();
const { getDictList } = useDict();
const dictObj = getDictList([
  "systemid",
]);
const searchData = reactive<FormOption[]>([
  {
    label: "关键字",
    element: "input",
    prop: "keyword",
  },
]);
const tableData = ref<RoomFace[]>([]);
const listQuery = ref<RoomFace>({
  current: 1,
  size: 10,
});
const total = ref<number>(0);

const onSearch = () => {
  listQuery.value.current = 1;
  getList();
};
const handleCreate = () => {
  id.value = undefined;
  type.value = "create";
  visible.value = true;
};
const handleEdit = (row: ProductFace) => {
  id.value = row.productId;
  type.value = "edit";
  visible.value = true;
};
const handleDelete = (row: any, index: number) => {
  ElMessageBox.confirm("确定要删除该产品?", "删除提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "error",
  })
    .then(() => {
      del(row.id).then((res) => {
        ElMessage({
          type: "success",
          message: "删除成功",
        });
        tableData.value.splice(index, 1);
      });
    })
    .catch(() => { });
};

const getList = () => {
  list(listQuery.value)
    .then((res) => {
      tableData.value = res.data.records;
      total.value = res.data.total;
    })
    .finally(() => {
      loading.value = false;
    });
};
getList();
</script>

<template>
  <div class="all-container">
    <div class="x-card all-pd">
      <div class="x-search">
        <SearchForm :formOptions="searchData" @onSearch="onSearch">
          <template #btns>
            <el-button @click="handleCreate">新增</el-button>
          </template>
        </SearchForm>
      </div>
      <div class="x-table">
        <el-table v-loading="loading" border center :data="tableData" row-key="productId" style="width: 100%">
          <el-table-column type="index" label="序号" />
          <el-table-column prop="name" label="名字" />

            <template #default="{ row, index }"> </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip prop="desc" label="描述" :width="200" />
          <el-table-column label="操作" :width="160">
            <template #default="{ row, $index }">
              <el-button link type="primary" size="small" @click="handleView(row)">查看</el-button>
              <el-button link type="warning" size="small" @click="handleEdit(row)">编辑</el-button>
              <el-button link type="danger" size="small" @click="handleDelete(row, $index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="x-pagination">
        <Pagination :total="total" @pagination="getList" v-model:page="listQuery.current"
          v-model:limit="listQuery.size" />
      </div>
    </div>
    <DialogForm :type="type" v-model="visible">
      <FormData @getList="getList" v-model="visible" v-if="visible" ref="formDataRef" :id="id" :type="type" />
    </DialogForm>
  </div>
</template>

<style lang="scss" scoped>
.subTable {
  padding: 10px;
  background-color: #efefef;
}
</style>
